昨天我們成功部署了 Rancher Server,但要讓整個數位牧場運作順暢,還需要把各個基礎設施串聯起來!今天我們要完善 PowerDNS 的內外部解析、設定 HAProxy 的多域名路由,以及優化 Rancher 的安全設定。這就像是為牧場建立完整的道路系統和安全管理制度,讓所有服務都能互相溝通!
PowerDNS Authoritative Server 無法直接處理遞迴查詢,需要安裝 PowerDNS Recursor:
# SSH 到 bastion 節點
ssh calvin@192.168.0.135
# 安裝 PowerDNS Recursor
sudo apt update
sudo apt install -y pdns-recursor
# 修改 PowerDNS 主服務端口避免衝突
sudo vim /etc/powerdns/pdns.conf
# 加入:
local-port=5300
# 重啟 PowerDNS
sudo systemctl restart pdns
# 編輯 Recursor 配置
sudo vim /etc/powerdns/recursor.conf
# 主要設定:
local-address=192.168.0.135
local-port=53
allow-from=127.0.0.0/8,192.168.0.0/16
forward-zones=ithome-rancher.duckdns.org.=127.0.0.1:5300
forward-zones-recurse=.=8.8.8.8;8.8.4.4
dnssec=off
# 啟動並設定開機啟動
sudo systemctl enable pdns-recursor
sudo systemctl start pdns-recursor
# 設定本機使用新的 DNS
sudo vim /etc/resolv.conf
# 修改為:
nameserver 192.168.0.135
# 測試解析
nslookup google.com
nslookup ithome-rancher.duckdns.org
如果遇到資料庫結構問題,需要先修復:
# 修復 MariaDB 資料庫結構
mysql -u pda -p pda
-- 添加缺少的欄位
ALTER TABLE domains ADD COLUMN options VARCHAR(64000) DEFAULT NULL;
ALTER TABLE domains ADD COLUMN catalog VARCHAR(255) DEFAULT NULL;
-- 修正 type 欄位長度
ALTER TABLE domains MODIFY COLUMN type VARCHAR(8) NOT NULL;
-- 檢查修正結果
DESCRIBE domains;
EXIT;
建立專用的域名區域來解析 Rancher 服務:
# 建立 ithome-rancher.duckdns.org 區域
sudo pdnsutil create-zone ithome-rancher.duckdns.org
# 設定 SOA 記錄
sudo pdnsutil replace-rrset ithome-rancher.duckdns.org @ SOA \
"ns1.ithome-rancher.duckdns.org. admin.ithome-rancher.duckdns.org. 1 3600 600 604800 300"
# 設定 NS 記錄和 glue 記錄
sudo pdnsutil add-record ithome-rancher.duckdns.org @ NS ns1.ithome-rancher.duckdns.org
sudo pdnsutil add-record ithome-rancher.duckdns.org ns1 A 127.0.0.1
# 設定內網 A 記錄(指向 bastion,透過 HAProxy 處理)
sudo pdnsutil add-record ithome-rancher.duckdns.org @ A 192.168.0.135
# 檢查設定結果
sudo pdnsutil list-zone ithome-rancher.duckdns.org
# 驗證記錄
nslookup ithome-rancher.duckdns.org localhost
我們需要讓所有 Rancher 節點都使用 bastion 作為 DNS 伺服器:
# 在每個節點上執行(rancher-server-1, rancher-master-1, rancher-worker-1)
sudo apt update
sudo apt install -y vim dnsutils
# 1. 編輯 systemd-resolved 設定
sudo vim /etc/systemd/resolved.conf
# 修改以下行:
DNS=192.168.0.135
# 2. 重新啟動 DNS 服務
sudo systemctl restart systemd-resolved
# 3. 驗證 DNS 設定
resolvectl status
nslookup ithome-rancher.duckdns.org
# SSH 到 bastion 節點
ssh calvin@192.168.0.135
# 備份現有配置
sudo cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
# 建立新的 HAProxy 配置
sudo tee /etc/haproxy/haproxy.cfg << 'EOF'
global
log stdout local0
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
defaults
mode http
log global
option httplog
option dontlognull
option log-health-checks
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
# HTTP 前端(重導向到 HTTPS)
frontend http_frontend
bind *:80
redirect scheme https code 301
# HTTPS 前端
frontend https_frontend
bind *:443 ssl crt /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
option forwardfor
# 基於 Host Header 的路由
acl rancher_host hdr(host) -i ithome-rancher.duckdns.org
# 路由規則
use_backend rancher_backend if rancher_host
# 預設後端
default_backend rancher_backend
# Rancher 後端
backend rancher_backend
balance roundrobin
option httpchk GET /healthz
http-check expect status 200
server rancher-server-1 192.168.0.116:443 ssl verify none check
EOF
# 測試配置語法
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# 重新啟動 HAProxy
sudo systemctl restart haproxy
sudo systemctl status haproxy
# 建立 SSL 憑證組合(如果還沒建立)
sudo mkdir -p /etc/ssl/certs/ithome-rancher
# 組合 fullchain 和 privkey(HAProxy 格式)
sudo cat /etc/letsencrypt/live/ithome-rancher.duckdns.org/fullchain.pem \
/etc/letsencrypt/live/ithome-rancher.duckdns.org/privkey.pem \
> /tmp/ithome-rancher.pem
sudo mv /tmp/ithome-rancher.pem /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
sudo chmod 600 /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
sudo chown root:root /etc/ssl/certs/ithome-rancher/ithome-rancher.pem
# 重新載入 HAProxy
sudo systemctl reload haproxy
# 測試 HTTP 重導向
curl -I http://ithome-rancher.duckdns.org
為什麼要改為 system-store?
透過 Rancher UI 設定:
strict
改為 system-store
為什麼要設定 TTL 為 0?
透過 Rancher UI 設定:
0
現在 HAProxy 已經配置完成,我們需要將本機的域名解析指向 bastion 節點:
# 在 MacBook 上編輯 hosts 檔案
sudo vim /etc/hosts
# 修改或新增以下行:
192.168.0.135 ithome-rancher.duckdns.org
# 驗證解析
ping -c 3 ithome-rancher.duckdns.org
# 建議不要加 PowerDNS 到 MacBook 的 DNS,離開網路就連不上了
# 寫 /etc/hosts 是比較穩定的做法
為什麼要改為 bastion IP?
完整的瀏覽器測試:
https://ithome-rancher.duckdns.org
預期結果:
今天我們完成了基礎設施的全面整合!PowerDNS 現在能正確解析內外部域名,HAProxy 可以根據域名路由流量,Rancher 也有了更安全的配置設定。
重點回顧:
明天我們將開始建立 Custom Cluster,學習如何透過 Rancher 管理多個 Kubernetes 叢集,真正體驗多叢集管理的威力!
💡 牧場主小提示:DNS 設定生效可能需要一些時間,如果遇到解析問題先檢查 systemd-resolved 的狀態!另外,HAProxy 的 SSL 憑證路徑要確保正確,否則 HTTPS 會失敗。記得定期檢查憑證到期時間並更新!